﻿using Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers.WaterManagement
{
    public class FlowMonController : Controller
    {
        //
        // GET: /FlowMon/
        ServiceReference1.Service1Client wsdcser = new ServiceReference1.Service1Client();
        ZHHQSERVICE.Service1Client service = new ZHHQSERVICE.Service1Client();

        public ActionResult Index()
        {
            return View();
        }

        #region 获取所有流水
        /// <summary>
        /// 获取所有流水
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllMon(JqGridParam jqgridparam)
        {
            List<FlowMon> list = new List<FlowMon>();
            string result = wsdcser.GetOrder_FlowMon(jqgridparam.page, jqgridparam.rows, "", "", "", "", "", GetYZM._YZM, 1);
            JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
            string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
            if (bs == "1")//返回结果正常
            {
                IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                list = ParseClass<FlowMon>.JsonList(jieguo);
                foreach (FlowMon item in list)
                   {
                       item.WaltFlowTime = DateTimeHelper.ToDate2(item.WaltFlowTime);
                       if (item.WaltFlowTime.Length == 14)
                       {
                           item.WaltFlowTime = DateTimeHelper.ToDate2(item.WaltFlowTime);
                       }
                   }
                var data = new
                {
                    total = searcho["total"].ToString(),
                    rows = list
                };
                return Json(data, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var date = new
                {
                    total = 0,
                    rows = list
                };
                return Json(date, JsonRequestBehavior.AllowGet);
            }
                #region
                //    if (jieguo.Count > 0)
            //    {
            //        foreach (JToken item in jieguo)
            //        {
            //            List<FlowMon> newlist = new List<FlowMon>();
            //            FlowMon m = new FlowMon();
            //            m.AccID = int.Parse(item["AccID"].ToString());
            //            m.AccName = item["AccName"].ToString();
            //            m.FlowMonID = item["FlowMonID"].ToString();
            //            m.WaltFlowTypeName = item["WaltFlowTypeName"].ToString();
            //            m.OrdrID = item["OrdrID"].ToString();
            //            m.PayID = item["PayID"].ToString();
            //            m.WaltBZ = item["WaltBZ"].ToString();
            //            m.WaltEndNub = DataHelper.MoneyToYuang(item["WaltEndNub"].ToString());
            //            m.WaltFlowOper = int.Parse(item["WaltFlowOper"].ToString());
            //            m.WaltFlowTime = item["WaltFlowTime"].ToString();
            //            m.WaltFlowType = item["WaltFlowType"].ToString();
            //            m.WaltHaveNub = DataHelper.MoneyToYuang(item["WaltHaveNub"].ToString());
            //            m.WaltID = item["WaltID"].ToString();
            //            m.WaltName = item["WaltName"].ToString();
            //            m.OperNum = item["OperNum"].ToString();
            //            m.WaltNowNub = DataHelper.MoneyToYuang(item["WaltNowNub"].ToString());
            //            m.AccCall = item["AccCall"].ToString();
            //            m.AccCerNub = item["AccCerNub"].ToString();
            //            m.AccCerType = item["AccCerType"].ToString();
            //            m.AccCerType_DicName_Login = item["AccCerType_DicName_Login"].ToString();
            //            m.AccIdentifier = item["AccIdentifier"].ToString();
            //            m.AccNum = item["AccNum"].ToString();
            //            //string accsex = item["AccSex"].ToString();
            //            m.AccSex = int.Parse(item["AccSex"].ToString());
            //            m.CategoryID = item["CategoryID"].ToString();
            //            m.CategoryID_DicName_SystemIdentity = item["CategoryID_DicName_SystemIdentity"].ToString();
            //            m.DepID = item["DepID"].ToString();
            //            m.DepID_DepName_Department = item["DepID_DepName_Department"].ToString();
            //            m.IdentityID = item["IdentityID"].ToString();
            //            m.StateID_DicName_SystemIdentity = item["StateID_DicName_SystemIdentity"].ToString();
            //            m.SataeID = item["SataeID"].ToString();
            //            m.PayID = item["PayID"].ToString();
            //            list.Add(m);
            //        }
            //    }
            //    return list;
            //}
            //else
            //{
            //    return null;
            //}
                #endregion
        }
        #endregion

        #region 获取证件类型
        /// <summary>
        /// 获取证件类型
        /// </summary>
        /// <returns></returns>
        public ActionResult getacccertype()
        {
            List<Basic_Dict_T> ls = new List<Basic_Dict_T>();
            ls = PublicMethod.GetDict.GetList(18);
            return Json(ls, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 获取组织结构
        /// <summary>
        /// 获取组织结构
        /// </summary>
        /// <returns></returns>
        public ActionResult getdep()
        {
            try
            {
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                List<TreeJsonEntity> TreeList = new List<TreeJsonEntity>();
                //int uid= user.OperID;
                string result = service.GetDepatMentNoValidate(GetYZM._YZM, 1);
                Model.DataResult dr = ParseClass<Model.DataResult>.StrDeserialize(result, 1);
                List<ZBZH_Manage_Order.Models.Basic_Department_T> ls = new List<ZBZH_Manage_Order.Models.Basic_Department_T>();
                JObject jb = JObject.Parse(result);
                if (jb["BS"].ToString() == "1")//返回结果正常
                {
                    IList<JToken> jieguo = jb["rows"].Children().ToList();
                    ls = ParseClass<ZBZH_Manage_Order.Models.Basic_Department_T>.JsonList(jieguo);//解析返回的结果
                    foreach (ZBZH_Manage_Order.Models.Basic_Department_T item in ls)
                    {
                        bool hasChildren = false;
                        IList childnode = ls.FindAll(t => t.DepParentID == item.DepID);
                        if (childnode.Count > 0)
                        {
                            hasChildren = true;
                        }
                        TreeJsonEntity tree = new TreeJsonEntity();
                        tree.id = item.DepID;
                        tree.text = item.DepName;
                        tree.value = item.DepID;
                        tree.isexpand = true;
                        tree.complete = true;
                        tree.hasChildren = hasChildren;
                        tree.parentId = item.DepParentID;
                        TreeList.Add(tree);
                    }
                    string a = TreeList.TreeToJson();
                    return Content(TreeList.TreeToJson());
                }
                else
                {
                    return Json(jb["Msg"].ToString());
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }
        #endregion

        #region 获取钱包到搜索栏
        /// <summary>
        /// 获取钱包到搜索栏
        /// </summary>
        /// <returns></returns>
        public ActionResult getwaltname()
        {
            try
            {
                List<Model.Order_Walt_T> list = new List<Model.Order_Walt_T>();//定义钱包
                string result = wsdcser.GetOrder_Walt("", 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    if (jieguo.Count > 0)
                    {
                        foreach (JToken item in jieguo)
                        {
                            Model.Order_Walt_T m = new Model.Order_Walt_T();
                            m.WaltID = item["WaltID"].ToString();
                            m.WaltName = item["WaltName"].ToString();
                            list.Add(m);
                        }
                    }
                    return Json(list, JsonRequestBehavior.AllowGet);
                }
                return Json(searcho["Msg"].ToString());
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }
        #endregion

        #region 获取流水类型
        /// <summary>
        /// 获取流水类型
        /// </summary>
        /// <returns></returns>
        public ActionResult getwaltflowtype()
        {
            List<Basic_Dict_T> ls = new List<Basic_Dict_T>();
            ls = PublicMethod.GetDict.GetList(30);
            return Json(ls, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 查询金额流水
        /// <summary>
        /// 查询金额流水
        /// </summary>
        /// <param name="AccName"></param>
        /// <param name="AccCall"></param>
        /// <param name="AccID"></param>
        /// <param name="AccCerType"></param>
        /// <param name="AccCerNub"></param>
        /// <param name="DepID"></param>
        /// <param name="WaltFlowType"></param>
        /// <param name="WaltName"></param>
        /// <param name="WaltFlowTime"></param>
        /// <param name="jqgridparam"></param>
        /// <returns></returns>
        public ActionResult SearchMon(string AccID, string WaltID, string WaltFlowType, string WaltFlowStime, string WaltFlowEtime, JqGridParam jqgridparam)
        //public ActionResult SearchMon(string AccName, JqGridParam jqgridparam)  
        {
            try
            {
                Page<FlowMon> page = new Page<FlowMon>();//声明实体类
                List<FlowMon> ls = new List<FlowMon>();
                string result = wsdcser.GetOrder_FlowMon(jqgridparam.page, jqgridparam.rows, AccID, WaltID, WaltFlowType, WaltFlowStime, WaltFlowEtime, GetYZM._YZM, 1);
                JObject search = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = search["BS"].ToString();//获取返回结果是否正常标识
               if(bs=="1"){
                   IList<JToken> jieguo = search["rows"].Children().ToList();//解析返回的结果
                   ls = ParseClass<FlowMon>.JsonList(jieguo);
                   foreach (FlowMon item in ls)
                   {
                       item.WaltFlowTime = DateTimeHelper.ToDate2(item.WaltFlowTime);
                       if (item.WaltFlowTime.Length == 14)
                       {
                           item.WaltFlowTime = DateTimeHelper.ToDate2(item.WaltFlowTime);
                       }
                   }
                    var data = new
                    {
                        total = search["total"].ToString(),
                        rows = ls//返回当前页的数据 
                    };
                    return Json(data);
                }
                else
                {
                    var data = new
                    {
                        total = 0,
                        rows = ls//返回当前页的数据 
                    };
                    return Json(data);
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
        #endregion

        #region 金额流水信息类定义（用于金额转换）
        /// <summary>
        /// 金额流水信息类定义（用于金额转换）
        /// </summary>
        public class FlowMon
        {
            public string AccCall { get; set; }
            public string AccCerNub { get; set; }
            public string AccCerType { get; set; }
            public string AccCerType_DicName_Login { get; set; }
            public int AccID { get; set; }
            public string AccIdentifier { get; set; }
            public string AccName { get; set; }
            public string AccNum { get; set; }
            public int? AccSex { get; set; }
            public string CategoryID { get; set; }
            public string CategoryID_DicName_SystemIdentity { get; set; }
            public string DepID { get; set; }
            public string DepID_DepName_Department { get; set; }
            public string FlowMonID { get; set; }
            public string IdentityID { get; set; }
            public string IdentityID_DiName_SystemIdentity { get; set; }
            public string OperNum { get; set; }
            public string OrdrID { get; set; }
            public string PayID { get; set; }
            public string SataeID { get; set; }
            public string StateID_DicName_SystemIdentity { get; set; }
            public string WaltBZ { get; set; }
            public int WaltEndNub { get; set; }
            public int WaltFlowOper { get; set; }
            public string WaltFlowTime { get; set; }
            public string WaltFlowType { get; set; }
            public string WaltFlowTypeName { get; set; }
            public int WaltHaveNub { get; set; }
            public string WaltID { get; set; }
            public string WaltName { get; set; }
            public int WaltNowNub { get; set; }
        }
        #endregion
    }
}
